package com.hidtechs.popfax.iconifiedList;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.MatrixCursor;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Toast;

import com.hidtechs.popfax.controller.Constants;
import com.hidtechs.popfax.view.DeleteContacts;
import com.hidtechs.popfax.view.DeleteFax;
import com.hidtechs.popfax.view.EditContacts;

/** @author Steven Osborn - http://steven.bitsetters.com */
public class ContactsTextListAdapter extends BaseAdapter  {

	/** Remember our context so we can use it when constructing views. */
	private Context mContext;
	private String fax_id;
	private ArrayList<String> itemSelected;
	 private static final String DISPLAY_NAME_COLUMN = "displayNameColumn";
	    private static final String IS_CHECKED_COLUMN = "isCheckedColumn";
	private ArrayList<ArrayList<String>> mItems = new ArrayList();

	public ContactsTextListAdapter(Context context) {
		mContext = context;
	}

	public void addItem(ArrayList<String> it) {
		mItems.add(it);
	}

	public void addButton(ArrayList<String> it) {
		mItems.add(it);
	}


	/** @return The number of items in the */
	public int getCount() { return mItems.size(); }

	public ArrayList<String> getItem(int position) { return mItems.get(position); }

	public boolean areAllItemsSelectable() { return false; }

	/*    public boolean isSelectable(int position) {
          try{
               return mItems.get(position).isSelectable();
          }catch (IndexOutOfBoundsException aioobe){
               return super.isEnabled(position);
          }
     }*/

	/** Use the array index as a unique id. */
	public long getItemId(int position) {
		return position;
	}

	/** @param convertView The old view to overwrite, if one is passed
	 * @returns a IconifiedTextView that holds wraps around an IconifiedText */
	public View getView(final int position, View convertView, ViewGroup parent) {
		ContactsTextView btv;
		
		if (convertView == null) {
			btv = new ContactsTextView(mContext, mItems.get(position));
			
			 if (position % 2 == 0 )
			{
				btv.setBackgroundColor(0xffdddddd);				
			}else
			{
				btv.setBackgroundColor(Color.WHITE);
			}	
			 btv.setScrollContainer(true);
			 btv.setFocusableInTouchMode(true);
			  if(position !=0)//not allow to click on header row
			 btv.setOnClickListener(new OnClickListener() {
					
					 public void onClick(View view) {
						 

							
					     CharSequence [] charSequences ={"Delete","Edit","Add","Cancel"};
						 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
						 builder.setCancelable(true);
						 builder.setTitle(Constants.ALERTDIALOG_DATA);
						 builder.setInverseBackgroundForced(true);
						/* builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
						   public void onClick(DialogInterface dialog, int which) {
							   
							   dialog.dismiss();
							   
						   }
						 });
						 builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
						   public void onClick(DialogInterface dialog, int which) {
						     dialog.dismiss();
						   }
						 });*/
						 
						 builder.setItems(charSequences, new DialogInterface.OnClickListener(){
					            public void onClick(DialogInterface dialog,  int which) {
							           if(which==0)
							           {	
							        	   dialog.dismiss();
							        	   DeleteContacts deleteContacts = new DeleteContacts();
							        	   deleteContacts.setContact_id(getItem(position).get(3));
										   Intent delIntent = new Intent(mContext.getApplicationContext(),DeleteContacts.class);
										   mContext.startActivity(delIntent);
							        	   Toast.makeText(mContext.getApplicationContext(), "Successfully Deleted", 
					                                Toast.LENGTH_SHORT).show();
							           }if(which==1)
							           {
							        	   dialog.dismiss();
							        	   EditContacts editContacts = new EditContacts();
							        	   editContacts.setContact_id(getItem(position).get(3));
							        	   editContacts.setContact_name(getItem(position).get(0));
							        	   editContacts.setContact_number(getItem(position).get(1));
										   Intent editIntent = new Intent(mContext.getApplicationContext(),EditContacts.class);
										   mContext.startActivity(editIntent);
							        	   Toast.makeText(mContext.getApplicationContext(), "Successfully Edited", 
					                                Toast.LENGTH_SHORT).show(); 
							           }if(which==2)
							           {
							        	   dialog.dismiss();
							        	   Toast.makeText(mContext.getApplicationContext(), "Add", 
					                                Toast.LENGTH_SHORT).show(); 
							           }if(which==3)
							           {
							        	   dialog.dismiss();
							        	    
							           }
							            	
							            
					            }});
						 
						 
						 AlertDialog alert = builder.create();
						 alert.show();
					 }
					});
				
			
		} else { // Reuse/Overwrite the View passed
			
			btv = (ContactsTextView) convertView;
			btv.setText(mItems.get(position));
			btv.setBackgroundColor(Color.WHITE);
			btv.setScrollContainer(true);

		}
		return btv;
	}

	
}
